"use strict";
var _configRead = require("../utils/config-read");
var _request = require("../utils/request");
let ConfigCenter = class ConfigCenter {
    async initial() {
        console.log('loading config...');
        this.configs.clear();
        const configRaws = await (0, _configRead).getRemoteConfigs();
        console.log('novel config loaded, current support site:', configRaws.map((i)=>i.site
        ));
        const worker = configRaws.map(async (config)=>{
            const key = config.site;
            try {
                const cookie = await (0, _request).getCookie(key, {
                    timeout: 8000
                });
                config.search.customHeader = JSON.stringify({
                    cookie
                });
            } catch (error) {
                console.log(`${key} - init cookie failed`);
            }
            this.supportedSites.push(key);
            this.configs.set(key, config);
        });
        Promise.all(worker).then(()=>{
            this.initialized = true;
            this.listeners.forEach((cb)=>{
                cb == null ? void 0 : cb();
            });
            this.listeners = [];
        });
    }
    addConfigListener(cb) {
        !this.initialized && this.listeners.push(cb);
    }
    constructor(){
        this.configs = new Map();
        this.supportedSites = [];
        this.initialized = false;
        this.listeners = [];
    }
};
module.exports = new ConfigCenter();
